//
//  SearchViewController.m
//  LastTemp
//
//  Created by apple on 15/4/22.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "SearchViewController.h"

@interface SearchViewController ()
@property(nonatomic,retain)UITableView *tableView;
@property(nonatomic,retain)UISearchBar *searchBar;
@property(nonatomic,retain)NSMutableArray *arr;

@end

@implementation SearchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.tableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] bounds] style:(UITableViewStylePlain)];
    [self.view addSubview:self.tableView];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    //下拉刷新
    [self.tableView addLegendHeaderWithRefreshingBlock:^{
        [self urlData];
    }];
    //上拉加载
    [self.tableView addLegendFooterWithRefreshingBlock:^{
        [self downAddData];
    }];
    
    self.searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, 30)];
    [self.view addSubview:self.searchBar];
    self.searchBar.delegate = self;
    [self.searchBar release];
    [self.tableView release];

}

//点击键盘搜索开始搜索
-(void)searchBarSearchButtonClicked:(UISearchBar *)searchBar
{
    [self urlData];
}

//网络请求
-(void)urlData
{
    NSString *str = [NSString stringWithFormat:@"http://so.ard.iyyin.com/s/song_with_out?q= %@ &page=1&size=50&app=ttpod&v=v7.7.0.2015012818&uid=&mid=iPhone4&f=f320&s=s310&imsi=&hid=&splus=7.1.1&active=1&net=2&openudid=ad6f50f586818c4f36ff4358053c1b53fee58cdf&idfa=E2EAC64B-1F1C-4AC5-A0E1-8BE37A03FB96&utdid=VTCyBQSkYzwDAALO9zZTFfPC&alf=201200",self.searchBar.text];
    NSString *strEcode = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURL *url = [NSURL URLWithString:strEcode];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        NSMutableDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        NSMutableArray *dataArr = [dic objectForKey:@"data"];
        self.arr = [NSMutableArray array];
        
        for (NSMutableDictionary *dataDic in dataArr) {
            SearchModel *singer = [[SearchModel alloc] init];
            [singer setValuesForKeysWithDictionary:dataDic];
            [self.arr addObject:singer];
            NSLog(@"%@",singer.song_name);
        }
        [self.tableView reloadData];
    }];

}

//下拉刷新
-(void)downAddData
{
    int page = 1;
    page++;
    NSString *str = [NSString stringWithFormat:@"http://so.ard.iyyin.com/s/song_with_out?q= %@ &page= %d &size=50&app=ttpod&v=v7.7.0.2015012818&uid=&mid=iPhone4&f=f320&s=s310&imsi=&hid=&splus=7.1.1&active=1&net=2&openudid=ad6f50f586818c4f36ff4358053c1b53fee58cdf&idfa=E2EAC64B-1F1C-4AC5-A0E1-8BE37A03FB96&utdid=VTCyBQSkYzwDAALO9zZTFfPC&alf=201200",self.searchBar.text,page];
    NSString *strEcode = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSURL *url = [NSURL URLWithString:strEcode];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        NSMutableDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        NSMutableArray *dataArr = [dic objectForKey:@"data"];
        self.arr = [NSMutableArray array];
        
        for (NSMutableDictionary *dataDic in dataArr) {
            SearchModel *singer = [[SearchModel alloc] init];
            [singer setValuesForKeysWithDictionary:dataDic];
            [self.arr addObject:singer];
            NSLog(@"%@",singer.song_name);
        }
        [self.tableView reloadData];
    }];

    
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.arr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *str = @"reuse";
    SearchCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
    if (cell == nil) {
        cell = [[SearchCell alloc] initWithStyle:(UITableViewCellStyleValue1) reuseIdentifier:str];
    }
    SearchModel *singer = self.arr[indexPath.row];
    cell.nameLabel.text = singer.song_name;
    cell.singerLabel.text = singer.singer_name;
    return cell;
}






- (void)addButtonView
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 30, self.view.frame.size.width/4, 50)];
    view.backgroundColor = [UIColor blackColor];
    [self.view addSubview:view];
    [view release];
    view.hidden = YES;
    if (self.colorNum == 1) {
        view.hidden = NO;
    }
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(self.view.frame.size.width/4*2, 0, self.view.frame.size.width/4, 50)];
    [view addSubview:label];
    [label release];
    label.text = @"搜索";
    label.textAlignment = NSTextAlignmentCenter;
    label.textColor = [UIColor colorWithRed:38/255.0 green:100/255.0 blue:200/255.0 alpha:1];
    label.font = [UIFont systemFontOfSize:15];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
